

''' <summary>
''' Represents Common Excel operations
''' </summary>
''' <remarks>The other part of this class can be found in the ExcelLateBoundOperations file</remarks>
Public Class ExcelOperations
    Implements IVBProjectProvider

#Region "Constants"

    ''' <summary>
    ''' Modes to open the workbook with
    ''' </summary>
    Public Enum WorkbookOpenMode

        ''' <summary>
        ''' The workbook will be read only
        ''' </summary>
        [ReadOnly] = 0

        ''' <summary>
        ''' The workbook will be read/write
        ''' </summary>
        ''' <remarks>If somebody else has the workbook open already, this will throw an exception</remarks>
        [ReadWrite] = 1

    End Enum

#End Region

#Region "Constructors"

    ''' <summary>
    ''' Creates the class, passing the workbook path we're going to use
    ''' </summary>
    ''' <param name="strWorkbookPath">The path to the workbook</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal strWorkbookPath As String, ByVal openMode As WorkbookOpenMode)

        ' Initialze our excel application
        InitializeExcel()

        ' Open the workbook
        OpenWorkbook(strWorkbookPath, openMode)

    End Sub

#End Region


#Region "IVBEProvider Implementation"


    ''' <summary>
    ''' Returns a refreence to the VB project contained in the excel workbook we just opened
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetVBProject() As Microsoft.Vbe.Interop.VBProject Implements IVBProjectProvider.GetVBProject

        Return Me.GetWorkbookVBProject()

    End Function

    Public Function GetComponentFileExtension(ByVal component As Microsoft.Vbe.Interop.VBComponent) As String Implements IVBProjectProvider.GetComponentFileExtension

        ' Determine the type of component it is
        Select Case component.Type
            Case Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_ClassModule

                Return "cls"

            Case Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule

                Return "bas"

            Case Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_MSForm

                Return "frm"

            Case Else

                ' If the component is the workbook, use WB
                If component.Name.Equals("ThisWorkbook", StringComparison.InvariantCultureIgnoreCase) Then

                    Return ".wbVBA"

                Else

                    Return ".wsVBA"

                End If

        End Select

    End Function

#End Region

End Class
